package com.qhit.controller;

import com.qhit.pojo.Goods;
import com.qhit.service.GoodsService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @RequiresPermissions("sssssssss:bbbbbbbbbb")
    @RequiresRoles("user")
    @RequestMapping("/list")
    public String list(Model m,String keywords,
                       @RequestParam(defaultValue = "1",required = false)
                       Integer page,
                       @RequestParam(defaultValue = "1",required = false)
                       Integer pageSize){
        Object principal = SecurityUtils.getSubject().getPrincipal();
        System.out.println(principal);
       Map map= goodsService.getListByPageWithKeywords(page,pageSize,keywords);

        m.addAttribute("goodsList",map.get("goodsList"));
        m.addAttribute("myParams",map.get("myParams"));
        return "goods/list";
    }
    @RequestMapping("toAdd")
    public String toAdd(){
        return "goods/add";
    }
    @RequestMapping(value = "/add",method =RequestMethod.POST)
    public String add(Goods goods, Model m,String doubleTime){
        if (doubleTime!=null&&!"".equals(doubleTime)){
            String[] split= doubleTime.split(" - ");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                goods.setStartDate(simpleDateFormat.parse(split[0]));
                goods.setEndDate(simpleDateFormat.parse(split[1]));
            } catch (ParseException e) {
                e.printStackTrace();
            }

        }
       int r= this.goodsService.add(goods);
        if (r>0){
            m.addAttribute("msg","添加成功！！");
        }else {
            m.addAttribute("msg","添加失败！！");
        }
        return "goods/msg";
    }
    @RequestMapping("/toEdit")
    public String toEdit(int id,Model model){
        Goods g=goodsService.getGoodsById(id);
        model.addAttribute("goods",g);
        return "goods/edit";
    }
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public String edit(Goods g,Model model,String doubleTime){
        if (doubleTime!=null&&!"".equals(doubleTime)){
            String[] split= doubleTime.split(" - ");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                g.setStartDate(simpleDateFormat.parse(split[0]));
                g.setEndDate(simpleDateFormat.parse(split[1]));
            } catch (ParseException e) {
                e.printStackTrace();
            }

        }
        int re=goodsService.edit(g);
        if (re>0){
            model.addAttribute("msg","修改成功！！");
        }else {
            model.addAttribute("msg","修改失败！！");
        }
        return "goods/msg";
    }
    @RequestMapping(value = "/del",method = RequestMethod.GET)
    public String del(int id,Model model){

        int re=goodsService.del(id);
        if (re>0){
            model.addAttribute("msg","删除成功！！");
        }else {
            model.addAttribute("msg","删除失败！！");
        }
        return "goods/msg";
    }

}
